package models;

import java.io.Serializable;

import play.libs.Json;
import util.StringUtil;

public abstract class Entity<K> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public abstract K getId();

	public abstract void setId(K id);

	public Object get(String nameOfAttribute) throws Exception {
		try {
			return getClass().getMethod(
					"get" + StringUtil.firstToUpper(nameOfAttribute)).invoke(
					this);
		} catch (Exception e) {
			throw new Exception("no se puede leer el campo " + nameOfAttribute,
					e);
		}
	}

	@SuppressWarnings("unchecked")
	public static <K> Class<? extends Entity<K>> getClass(String clazz)
			throws Exception {
		try {
			return (Class<? extends Entity<K>>) Class
					.forName("models." + clazz);
		} catch (ClassNotFoundException e) {
			throw new Exception("no se puede localizar la clase " + clazz);
		}
	}

	public String toString() {
		return Json.toJson(this).toString();
	}

}
